<?php
//日本語
class PostsController extends AppController {

	var $name = 'Posts';
	var $uses = array('Post','User');
	var $scaffold;
	var $components = array('AdminAuth', 'Pmaimai', 'RequestHandler', 'Name');
	var $protectedActions = array('index', 'delete');
	var $helpers = array('Html', 'Form');

	function add()
	{
		$this->set('error', false);
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		if(!$user){
			$this->redirect('/');
		}
		$this->User->set($user);
		$this->set('user', $user);
		
		if(!isset($this->data['Post'])){
			return;
		}
		
		if(empty($this->data['Post']['title']) || empty($this->data['Post']['content']) || empty($this->data['Post']['control']) ){
			$this->Post->invalidate('empty', '未入力箇所があります');
		}
		
		if(!$this->Post->register($userID, $this->data['Post']['title'], $this->data['Post']['content'], $this->data['Post']['control'])){
			$this->set('error', true);
			$this->set('errorMsg', $this->Post->validationErrors);
			return;
		}
		$this->flash("日記を追加しました", array('controller' => 'members', 'action' => 'post'));
	}
	
	function changecontrol()
	{
		$this->set('error', false);
		$this->Pmaimai->checkMemberAuth();
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		if(!$user){
			$this->redirect('/');
		}
		$this->User->set($user);
		$this->set('user', $user);
		
		if(!isset($this->data['Post'])){
			return;
		}
		
		if(empty($this->data['Post']['control'])){
			$this->User->invalidate('range', '公開範囲が未選択です');
		}
		
		if(!$this->User->changePostControl($this->data['Post']['control'])){
			$this->set('error', true);
			$this->set('errorMsg', $this->User->validationErrors);
			return;
		}
		$this->flash("日記のデフォルト公開範囲を変更しました", array('controller' => 'members', 'action' => 'post'));

	}
	
	function view($id = null)
	{
		$this->Pmaimai->clean($id);
		$this->Pmaimai->checkMemberAuth();
		
		if(!$id || !is_numeric($id)){
			$this->redirect('/');
		}
		
		$this->set('error', false);
		$userID = $this->Session->read($this->sessionUserName);
		
		//見られる権限かどうか あとで
		
		$bindposts = array(
			'hasMany' => array(
				'Comment' => $this->Post->hasMany['Comment'],
			),
		);
		$this->Post->unbindFully();
		$this->Post->bindModel($bindposts);
		$this->Post->recursive = 1;
		$post = $this->Post->findByIdAndStatus($id, 'active');
		$this->set('post', $post);
		
		
		
	}
	
	function edit($id = null)
	{
		$this->Pmaimai->clean($id);
		$this->Pmaimai->checkMemberAuth();
		
		if(!$id || !is_numeric($id)){
			$this->redirect('/');
		}
		
		$this->set('error', false);
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->Post->unbindFully();
		$post = $this->Post->findByIdAndStatus($id, 'active');
		if(!$post || $post['Post']['user_id'] !== $userID){
			$this->flash("編集できません", array('controller' => 'members', 'action' => 'post'));
			return;
		}
		$this->set('post', $post);
		$this->Post->set($post);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		if(!$user){
			$this->redirect('/');
		}
		$this->set('user', $user);
		
		if(!isset($this->data['Post'])){
			return;
		}
		
		if(empty($this->data['Post']['title']) || empty($this->data['Post']['content']) || empty($this->data['Post']['control']) ){
			$this->Post->invalidate('empty', '未入力箇所があります');
		}
		if(!$this->Post->modify($this->data['Post']['title'], $this->data['Post']['content'], $this->data['Post']['control'])){
			$this->set('error', true);
			$this->set('errorMsg', $this->Post->validationErrors);
			return;
		}
		$this->flash("日記を編集しました", array('controller' => 'members', 'action' => 'post'));

	}
	function eliminate($id = null)
	{
		$this->Pmaimai->clean($id);
		$this->Pmaimai->checkMemberAuth();
		
		if(!$id || !is_numeric($id)){
			$this->redirect('/');
		}
		
		$this->set('error', false);
		$userID = $this->Session->read($this->sessionUserName);
		
		$this->Post->unbindFully();
		$post = $this->Post->findByIdAndStatus($id, 'active');
		if(!$post || $post['Post']['user_id'] !== $userID){
			$this->flash("削除できません", array('controller' => 'members', 'action' => 'post'));
			return;
		}
		$this->set('post', $post);
		$this->Post->set($post);
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		if(!$user){
			$this->redirect('/');
		}
		$this->set('user', $user);
		
		if(!isset($this->data['Post'])){
			return;
		}
		
		if(empty($this->data['Post']['agree']) || $this->data['Post']['agree'] !== '1'){
			$this->Post->invalidate('agree', '削除しますにチェックを入れてください');
		}
		
		if(!$this->Post->updateStatus('disactivate')){
			$this->set('error', true);
			$this->set('errorMsg', $this->Post->validationErrors);
			return;
		}
		$this->flash("日記を削除しました", array('controller' => 'members', 'action' => 'post'));
	}

}
?>